﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ThridGroup.ERP.DTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.ProductionSystem
{
    public class CustomerServices : ApplicationService, ICustomerServices
    {
        private readonly IRepository<CustomerModel, int> _customer;

        public CustomerServices(IRepository<CustomerModel, int> customer)
        {
            _customer = customer;
        }

        public async Task<int> AddCustomer(CustomerDTO customer)
        {
            var entity = ObjectMapper.Map<CustomerDTO, CustomerModel>(customer);
            var result = await _customer.InsertAsync(entity);
            return result == null ? 1 : 0;
        }

        public async Task<int> DeleteCustomer(int id)
        {
            var result = _customer.DeleteAsync(m => m.Id == id);
            return result == null ? 1 : 0;
        }

        public async Task DelManyCustomer(List<int> ids)
        {

            await _customer.DeleteDirectAsync(m => ids.Contains(m.Id));

        }

        public async Task<CustomerDTO> QueryCustomer(int id)
        {
            var customer = await _customer.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<CustomerModel, CustomerDTO>(customer);
            return result;
        }

        public async Task<List<CustomerDTO>> ShowCustomer(int pageIndex = 1, int pageSize = 10, string? customerName = "")
        {
            var customerList = await _customer.GetListAsync();
            customerList = customerList.WhereIf(!string.IsNullOrEmpty(customerName), m => m.CustomerName.Contains(customerName)).ToList();
            var result = ObjectMapper.Map<List<CustomerModel>, List<CustomerDTO>>(customerList);
            return result;
        }

        public async Task<int> UpdateCustomer(CustomerDTO customer)
        { 
            var entity = ObjectMapper.Map<CustomerDTO, CustomerModel>(customer);
            var result = await _customer.UpdateAsync(entity);
            return result == null ? 1 : 0;
        }
    }
}
